package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db1.*;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.*;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.CommonUtils;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class RoadmapSmallTypeServiceImpl implements RoadmapSmallTypeService {

    @Autowired
    private RoadmapSmallTypeDao roadmapSmallTypeDao;

    @Autowired
    private RoadmapBigTypeDao roadmapBigTypeDao;

    @Autowired
    private RoadmapParamDao roadmapParamDao;

    @Autowired
    private RoadmapH3cDao roadmapH3cDao;

    @Autowired
    private RoadmapCmDao roadmapCmDao;

    @Autowired
    private RoadmapCmStatusDao roadmapCmStatusDao;

    @Autowired
    private PcbaCompanyDao pcbaCompanyDao;

    @Autowired
    private CommonJudgeCanDeleteService commonService;

    @Autowired
    private CoreAlgorithmService coreAlgorithmService;

    @Value("${personal.config.amazon-s3.url}")
    private String url;
    @Value("${personal.config.amazon-s3.file-bucket}")
    private String fileBucket;
    @Value("${personal.config.env}")
    private String env;

    @Autowired
    private OperateService operateService;

    private String operateSmallType = "Roadmap小类";

    @Override
    public List<RoadmapSmallType> getAllRoadmapSmallType(String searchValue, Integer roadmapBigTypeId) {
        List<RoadmapSmallType> list = roadmapSmallTypeDao.getAllRoadmapSmallType(searchValue, roadmapBigTypeId);
        for (RoadmapSmallType roadmapSmallType : list) {
            roadmapSmallType.setRoadmapBigType(roadmapBigTypeDao.getRoadmapBigTypeById(roadmapSmallType.getRoadmapBigTypeId()));
        }
        return list;
    }

    @Override
    public Result deleteRoadmapSmallType(Integer roadmapSmallTypeId) {
        try {
            if (roadmapSmallTypeId == null || "".equals(roadmapSmallTypeId.toString())) {
                return ResultUtil.error(500, "缺少参数roadmapSmallTypeId", "Miss Param roadmapSmallTypeId", null);
            }
            Map<String, Integer> stringIntegerMap = commonService.judgeCanDeleteRoadmapSmallType(roadmapSmallTypeId);
            if (stringIntegerMap.get("roadmapParamNum") > 0) {
                return ResultUtil.error(500,
                        "禁止删除，当前项"
                                + stringIntegerMap.get("roadmapParamNum") + "处关联roadmap能力项",
                        "Forbidden Delete", null);
            }

            String userId = UserHeaderHolder.getUserId();
            RoadmapSmallType oldRoadmapSmallType = roadmapSmallTypeDao.getRoadmapSmallTypeById(roadmapSmallTypeId);

            Integer integer = roadmapSmallTypeDao.deleteRoadmapSmallType(roadmapSmallTypeId);
            if (integer > 0) {

                // 操作日志
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 2, 1, new Date(), JSONObject.toJSONString(oldRoadmapSmallType), null));

                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result addOrUpdateRoadmapSmallType(String mode, RoadmapSmallType roadmapSmallType) {
        try {
            if (roadmapSmallType == null) {
                return ResultUtil.error(500, "缺少参数roadmapSmallType", "Miss Param roadmapSmallType", null);
            }
            if (mode == null || "".equals(mode)) {
                return ResultUtil.error(500, "缺少参数mode", "Miss Param mode", null);
            }
            if (!("edit".equals(mode) || "add".equals(mode))) {
                return ResultUtil.error(500, "mode 参数错误", "mode Param Error", null);
            }

            String userId = UserHeaderHolder.getUserId();

            if ("edit".equals(mode)) {
                if (roadmapSmallType.getRoadmapSmallTypeId() == null || "".equals(roadmapSmallType.getRoadmapSmallTypeId().toString())) {
                    return ResultUtil.error(500, "缺少参数roadmapSmallTypeId", "Miss Param roadmapSmallTypeId", null);
                }

                RoadmapSmallType oldRoadmapSmallType = roadmapSmallTypeDao.getRoadmapSmallTypeById(roadmapSmallType.getRoadmapSmallTypeId());

                roadmapSmallType.setUpdateTime(new Date());
                Integer integer = roadmapSmallTypeDao.updateRoadmapSmallType(roadmapSmallType);
                if (integer > 0) {

                    // 操作日志
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(oldRoadmapSmallType), JSONObject.toJSONString(roadmapSmallType)));

                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            } else {
                roadmapSmallType.setCreateTime(new Date());
                Integer integer = roadmapSmallTypeDao.addRoadmapSmallType(roadmapSmallType);
                if (integer > 0) {

                    // 操作日志
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 1, 1, new Date(), null, JSONObject.toJSONString(roadmapSmallType)));

                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result getAllRoadmapSmallTypeAndParam(Integer roadmapBigTypeId, Integer year, Integer versionY, String versionN) {
        try {
            if (roadmapBigTypeId == null || "".equals(roadmapBigTypeId.toString())) {
                return ResultUtil.error(500, "缺少参数roadmapBigTypeId", "Miss Param roadmapBigTypeId", null);
            }
            if (year == null || "".equals(year.toString())) {
                return ResultUtil.error(500, "缺少参数year", "Miss Param year", null);
            }
            if (versionY == null || "".equals(versionY.toString())) {
                return ResultUtil.error(500, "缺少参数versionY", "Miss Param versionY", null);
            }
            if (versionN == null || "".equals(versionN)) {
                return ResultUtil.error(500, "缺少参数versionN", "Miss Param versionN", null);
            }
            List<RoadmapSmallType> list = roadmapSmallTypeDao.getAllRoadmapSmallType(null, roadmapBigTypeId);
            for (RoadmapSmallType roadmapSmallType : list) {
                List<RoadmapParam> allRoadmapParam = roadmapParamDao.getAllRoadmapParam(null, roadmapBigTypeId, roadmapSmallType.getRoadmapSmallTypeId());
                for (RoadmapParam roadmapParam : allRoadmapParam) {
                    // 循环匹配 h3c能力
                    List<RoadmapH3c> allRoadmapH3c = roadmapH3cDao.getAllRoadmapH3c(year, versionY, versionN);
                    for (int a = 0; a < allRoadmapH3c.size(); a++) {
                        if (allRoadmapH3c.get(a).getRoadmapH3cParamKey().equals(roadmapParam.getRoadmapParamKey())) {
                            roadmapParam.setValue1(allRoadmapH3c.get(a).getRoadmapH3cParamValue1());
                            roadmapParam.setValue2(allRoadmapH3c.get(a).getRoadmapH3cParamValue2());
                            roadmapParam.setValue3(allRoadmapH3c.get(a).getRoadmapH3cParamValue3());
                            break;
                        }
                    }
                }
                roadmapSmallType.setRoadmapParamList(allRoadmapParam);
            }
            return ResultUtil.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result getAllRoadmapSmallTypeAndParamH3c(Integer roadmapBigTypeId, Integer year, Integer versionY, String versionN) {
        try {
            if (roadmapBigTypeId == null || "".equals(roadmapBigTypeId.toString())) {
                return ResultUtil.error(500, "缺少参数roadmapBigTypeId", "Miss Param roadmapBigTypeId", null);
            }
            if (year == null || "".equals(year.toString())) {
                return ResultUtil.error(500, "缺少参数year", "Miss Param year", null);
            }
            if (versionY == null || "".equals(versionY.toString())) {
                return ResultUtil.error(500, "缺少参数versionY", "Miss Param versionY", null);
            }
            if (versionN == null || "".equals(versionN)) {
                return ResultUtil.error(500, "缺少参数versionN", "Miss Param versionN", null);
            }
            List<RoadmapSmallType> list = roadmapSmallTypeDao.getAllRoadmapSmallType(null, roadmapBigTypeId);
            for (RoadmapSmallType roadmapSmallType : list) {
                List<RoadmapParam> allRoadmapParam = roadmapParamDao.getAllRoadmapParam(null, roadmapBigTypeId, roadmapSmallType.getRoadmapSmallTypeId());
                for (RoadmapParam roadmapParam : allRoadmapParam) {

                    if (roadmapParam.getRoadmapParamPicture() != null && !"".equals(roadmapParam.getRoadmapParamPicture())) {
                        if (env != null && env.contains("pro"))  {
                            roadmapParam.setRoadmapParamPictureBase64(CommonUtils.base64Prefix() + CommonUtils.getImageBase64FromUrl(url + "/" + fileBucket + "/" + roadmapParam.getRoadmapParamPicture()));
                        } else {
                            roadmapParam.setRoadmapParamPictureBase64(CommonUtils.base64Prefix() + CommonUtils.getImageBase64FromUrl(CommonUtils.returnHostPrefixUrl() + roadmapParam.getRoadmapParamPicture()));
                        }
                    }

                    RoadmapH3c roadmapH3c = roadmapH3cDao.getRoadmapH3cByKey(roadmapParam.getRoadmapParamKey(), year, versionY, versionN);
                    if (roadmapH3c != null) {
                        roadmapParam.setValueH3C1(roadmapH3c.getRoadmapH3cParamValue1());
                        roadmapParam.setValueH3C2(roadmapH3c.getRoadmapH3cParamValue2());
                        roadmapParam.setValueH3C3(roadmapH3c.getRoadmapH3cParamValue3());
                    }


                    RoadmapH3c roadmapH3c2 = roadmapH3cDao.getRoadmapH3cByKey(roadmapParam.getRoadmapParamKey(), year + 1, versionY, versionN);
                    if (roadmapH3c2 != null) {
                        roadmapParam.setValueH3C1Add1Year(roadmapH3c2.getRoadmapH3cParamValue1());
                        roadmapParam.setValueH3C2Add1Year(roadmapH3c2.getRoadmapH3cParamValue2());
                        roadmapParam.setValueH3C3Add1Year(roadmapH3c2.getRoadmapH3cParamValue3());
                    }

                    RoadmapH3c roadmapH3c3 = roadmapH3cDao.getRoadmapH3cByKey(roadmapParam.getRoadmapParamKey(), year + 2, versionY, versionN);
                    if (roadmapH3c3 != null) {
                        roadmapParam.setValueH3C1Add2Year(roadmapH3c3.getRoadmapH3cParamValue1());
                        roadmapParam.setValueH3C2Add2Year(roadmapH3c3.getRoadmapH3cParamValue2());
                        roadmapParam.setValueH3C3Add2Year(roadmapH3c3.getRoadmapH3cParamValue3());
                    }
                }
                roadmapSmallType.setRoadmapParamList(allRoadmapParam);
            }
            return ResultUtil.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result getAllRoadmapSmallTypeAndParamCm(Integer roadmapBigTypeId, Integer pcbaCompanyId, Integer year, Integer versionY, String versionN) {
        try {
            if (roadmapBigTypeId == null || "".equals(roadmapBigTypeId.toString())) {
                return ResultUtil.error(500, "缺少参数roadmapBigTypeId", "Miss Param roadmapBigTypeId", null);
            }
            if (pcbaCompanyId == null || "".equals(pcbaCompanyId.toString())) {
                return ResultUtil.error(500, "缺少参数pcbaCompanyId", "Miss Param pcbaCompanyId", null);
            }
            if (year == null || "".equals(year.toString())) {
                return ResultUtil.error(500, "缺少参数year", "Miss Param year", null);
            }
            if (versionY == null || "".equals(versionY.toString())) {
                return ResultUtil.error(500, "缺少参数versionY", "Miss Param versionY", null);
            }
            if (versionN == null || "".equals(versionN)) {
                return ResultUtil.error(500, "缺少参数versionN", "Miss Param versionN", null);
            }
            List<RoadmapSmallType> list = roadmapSmallTypeDao.getAllRoadmapSmallType(null, roadmapBigTypeId);
            for (RoadmapSmallType roadmapSmallType : list) {
                List<RoadmapParam> allRoadmapParam = roadmapParamDao.getAllRoadmapParam(null, roadmapBigTypeId, roadmapSmallType.getRoadmapSmallTypeId());
                for (RoadmapParam roadmapParam : allRoadmapParam) {

                    if (roadmapParam.getRoadmapParamPicture() != null && !"".equals(roadmapParam.getRoadmapParamPicture())) {
                        if (env != null && env.contains("pro"))  {
                            roadmapParam.setRoadmapParamPictureBase64(CommonUtils.base64Prefix() + CommonUtils.getImageBase64FromUrl(url + "/" + fileBucket + "/" + roadmapParam.getRoadmapParamPicture()));
                        } else {
                            roadmapParam.setRoadmapParamPictureBase64(CommonUtils.base64Prefix() + CommonUtils.getImageBase64FromUrl(CommonUtils.returnHostPrefixUrl() + roadmapParam.getRoadmapParamPicture()));
                        }
                    }

                    // 根据 key 和 工厂id 查询
                    RoadmapCm roadmapCm = roadmapCmDao.getRoadmapCmByCompanyAndKey(pcbaCompanyId, roadmapParam.getRoadmapParamKey(), versionY, versionN);
                    if (roadmapCm != null) {
                        roadmapParam.setValue2(roadmapCm.getRoadmapCmParamValue2());
                        roadmapParam.setValue3(roadmapCm.getRoadmapCmParamValue3());
                        roadmapParam.setCauses1(roadmapCm.getAbilityMismatchCauses1());
                        roadmapParam.setPlan1(roadmapCm.getAbilityImprovePlan1());
                        roadmapParam.setCauses2(roadmapCm.getAbilityMismatchCauses2());
                        roadmapParam.setPlan2(roadmapCm.getAbilityImprovePlan2());
                        roadmapParam.setCauses3(roadmapCm.getAbilityMismatchCauses3());
                        roadmapParam.setPlan3(roadmapCm.getAbilityImprovePlan3());
                    }

                    // 循环匹配 cm能力
                    // value1 取值 如配置的规则（需要来自规则，不能从数据库读取），否则从数据库读取
                    String finalValue = "";
                    if (roadmapParam.getTechDeviceParamKeys() == null || "".equals(roadmapParam.getTechDeviceParamKeys())) {
                        if (roadmapCm != null) {
                            finalValue = roadmapCm.getRoadmapCmParamValue1();
                        }
                    } else {
                        Result result = coreAlgorithmService.calcRoadmapCmParamValue1(pcbaCompanyId, roadmapParam.getRoadmapParamKey());
                        if (result.getCode() == 200) {
                            finalValue = result.getData().toString();
                        }
                    }
                    roadmapParam.setValue1(finalValue);

                    String finalIsMatch1 = "否";
                    String finalIsMatch2 = "否";
                    String finalIsMatch3 = "否";
                    RoadmapH3c roadmapH3c = roadmapH3cDao.getRoadmapH3cByKey(roadmapParam.getRoadmapParamKey(), year, versionY, versionN);
                    if (roadmapH3c != null) {
                        /*if(roadmapH3c.getRoadmapH3cParamValue1().equals("7.5")){
                            System.out.println(11111);
                        }*/
                        Result result2 = coreAlgorithmService.calcRoadmapCmIsMatchH3c(roadmapParam.getValueAlgorithmType(), roadmapParam.getRuleAlgorithmType(), roadmapH3c.getRoadmapH3cParamValue1(), finalValue);
                        if (result2.getCode() == 200) {
                            finalIsMatch1 = result2.getData().toString();
                        }
                        roadmapParam.setIsMatch1(finalIsMatch1);

                        Result result2_ = coreAlgorithmService.calcRoadmapCmIsMatchH3c(roadmapParam.getValueAlgorithmType(), roadmapParam.getRuleAlgorithmType(), roadmapH3c.getRoadmapH3cParamValue2(), roadmapCm != null ? roadmapCm.getRoadmapCmParamValue2() : null);
                        if (result2_.getCode() == 200) {
                            finalIsMatch2 = result2_.getData().toString();
                        }
                        roadmapParam.setIsMatch2(finalIsMatch2);


                        Result result2__ = coreAlgorithmService.calcRoadmapCmIsMatchH3c(roadmapParam.getValueAlgorithmType(), roadmapParam.getRuleAlgorithmType(), roadmapH3c.getRoadmapH3cParamValue3(), roadmapCm != null ? roadmapCm.getRoadmapCmParamValue3() : null);
                        if (result2__.getCode() == 200) {
                            finalIsMatch3 = result2__.getData().toString();
                        }
                        roadmapParam.setIsMatch3(finalIsMatch3);

                        roadmapParam.setValueH3C1(roadmapH3c.getRoadmapH3cParamValue1());
                        roadmapParam.setValueH3C2(roadmapH3c.getRoadmapH3cParamValue2());
                        roadmapParam.setValueH3C3(roadmapH3c.getRoadmapH3cParamValue3());
                    }


                    RoadmapH3c roadmapH3c2 = roadmapH3cDao.getRoadmapH3cByKey(roadmapParam.getRoadmapParamKey(), year + 1, versionY, versionN);
                    if (roadmapH3c2 != null) {
                        roadmapParam.setValueH3C1Add1Year(roadmapH3c2.getRoadmapH3cParamValue1());
                        roadmapParam.setValueH3C2Add1Year(roadmapH3c2.getRoadmapH3cParamValue2());
                        roadmapParam.setValueH3C3Add1Year(roadmapH3c2.getRoadmapH3cParamValue3());
                    }

                    RoadmapH3c roadmapH3c3 = roadmapH3cDao.getRoadmapH3cByKey(roadmapParam.getRoadmapParamKey(), year + 2, versionY, versionN);
                    if (roadmapH3c3 != null) {
                        roadmapParam.setValueH3C1Add2Year(roadmapH3c3.getRoadmapH3cParamValue1());
                        roadmapParam.setValueH3C2Add2Year(roadmapH3c3.getRoadmapH3cParamValue2());
                        roadmapParam.setValueH3C2Add2Year(roadmapH3c3.getRoadmapH3cParamValue3());
                    }
                }
                roadmapSmallType.setRoadmapParamList(allRoadmapParam);
            }
            return ResultUtil.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result getCmRoadmapByCompanyIdsAndBigTypeAndCalcCapacityIsMatch(Integer roadmapBigTypeId, String pcbaCompanyIdsStr, Integer year, Integer versionY, String versionN) {
        try {
            if (pcbaCompanyIdsStr == null || "".equals(pcbaCompanyIdsStr)) {
                return ResultUtil.error(500, "缺少参数pcbaCompanyIdsStr", "Miss Param pcbaCompanyIdsStr", null);
            }
            if (roadmapBigTypeId == null || "".equals(roadmapBigTypeId.toString())) {
                return ResultUtil.error(500, "缺少参数roadmapBigTypeId", "Miss Param roadmapBigTypeId", null);
            }
            if (year == null || "".equals(year.toString())) {
                return ResultUtil.error(500, "缺少参数year", "Miss Param year", null);
            }
            if (versionY == null || "".equals(versionY.toString())) {
                return ResultUtil.error(500, "缺少参数versionY", "Miss Param versionY", null);
            }
            if (versionN == null || "".equals(versionN.toString())) {
                return ResultUtil.error(500, "缺少参数versionN", "Miss Param versionN", null);
            }
            String[] companyIdsArr = pcbaCompanyIdsStr.split(",");
            List<List<RoadmapParam>> finalList = new ArrayList<>();

            List<RoadmapParam> allRoadmapParam = roadmapParamDao.getAllRoadmapParam(null, roadmapBigTypeId, null);
            // 先得到h3c的能力
            for (RoadmapParam roadmapParam : allRoadmapParam) {
                RoadmapBigType roadmapBigTypeById = roadmapBigTypeDao.getRoadmapBigTypeById(roadmapParam.getRoadmapBigTypeId());
                if (roadmapBigTypeById != null) {
                    roadmapParam.setRoadmapBigTypeName(roadmapBigTypeById.getRoadmapBigTypeName());
                }
                RoadmapSmallType roadmapSmallTypeById = roadmapSmallTypeDao.getRoadmapSmallTypeById(roadmapParam.getRoadmapSmallTypeId());
                if (roadmapSmallTypeById != null) {
                    roadmapParam.setRoadmapSmallTypeName(roadmapSmallTypeById.getRoadmapSmallTypeName());
                }

                RoadmapH3c roadmapH3c = roadmapH3cDao.getRoadmapH3cByKey(roadmapParam.getRoadmapParamKey(), year, versionY, versionN);
                if (roadmapH3c != null) {
                    roadmapParam.setValue1(roadmapH3c.getRoadmapH3cParamValue1());
                    roadmapParam.setValue2(roadmapH3c.getRoadmapH3cParamValue2());
                    roadmapParam.setValue3(roadmapH3c.getRoadmapH3cParamValue3());
                }
            }
            finalList.add(allRoadmapParam);

            for (String s : companyIdsArr) {
                if (!CommonUtils.isNumeric(s)) {
                    return ResultUtil.error(500, "错误参数pcbaCompanyIdsStr", "Error Param pcbaCompanyIdsStr", null);
                }
                allRoadmapParam = roadmapParamDao.getAllRoadmapParam(null, roadmapBigTypeId, null);
                PcbaCompany pcbaCompany = pcbaCompanyDao.getPcbaCompanyById(Integer.parseInt(s));
                for (RoadmapParam roadmapParam : allRoadmapParam) {
                    roadmapParam.setCompanyName(pcbaCompany.getPcbaCompanyNameCn());
                    //roadmapParam.setRoadmapBigType(roadmapBigTypeDao.getRoadmapBigTypeById(roadmapParam.getRoadmapBigTypeId()));
                    //roadmapParam.setRoadmapSmallType(roadmapSmallTypeDao.getRoadmapSmallTypeById(roadmapParam.getRoadmapSmallTypeId()));
                    // 循环匹配 cm 能力
                    // value1 需要取自规则，不能通过查询数据库
                    String finalValue = "";
                    Result result = coreAlgorithmService.calcRoadmapCmParamValue1(Integer.parseInt(s), roadmapParam.getRoadmapParamKey());
                    if (result.getCode() == 200) {
                        finalValue = result.getData().toString();
                    }
                    roadmapParam.setValue1(finalValue);

                    RoadmapH3c roadmapH3c = roadmapH3cDao.getRoadmapH3cByKey(roadmapParam.getRoadmapParamKey(), year, versionY, versionN);
                    String finalIsMatch1 = "否";
                    if (roadmapH3c != null) {
                        Result result2 = coreAlgorithmService.calcRoadmapCmIsMatchH3c(roadmapParam.getValueAlgorithmType(), roadmapParam.getRuleAlgorithmType(), roadmapH3c.getRoadmapH3cParamValue1(), finalValue);
                        if (result2.getCode() == 200) {
                            finalIsMatch1 = result2.getData().toString();
                        }
                    }

                    RoadmapCm roadmapCm = roadmapCmDao.getRoadmapCmByCompanyAndKey(Integer.parseInt(s), roadmapParam.getRoadmapParamKey(), versionY, versionN);
                    if (roadmapCm != null) {
                        roadmapParam.setValue2(roadmapCm.getRoadmapCmParamValue2());
                        roadmapParam.setValue3(roadmapCm.getRoadmapCmParamValue3());
                        roadmapParam.setCauses1(roadmapCm.getAbilityMismatchCauses1());
                        roadmapParam.setPlan1(roadmapCm.getAbilityImprovePlan1());
                        roadmapParam.setCauses2(roadmapCm.getAbilityMismatchCauses2());
                        roadmapParam.setPlan2(roadmapCm.getAbilityImprovePlan2());
                        roadmapParam.setCauses3(roadmapCm.getAbilityMismatchCauses3());
                        roadmapParam.setPlan3(roadmapCm.getAbilityImprovePlan3());
                        roadmapParam.setIsMatch1(finalIsMatch1);
                    } else {
                        roadmapParam.setIsMatch1(finalIsMatch1);
                    }
                }
                finalList.add(allRoadmapParam);
            }
            return ResultUtil.success(finalList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result getCmRoadmapByCompanyIdsAndBigTypeAndCalcCapacityIsMatch2(Integer roadmapBigTypeId, String pcbaCompanyIdsStr, Integer year, Integer versionY, String versionN) {
        try {
            if (roadmapBigTypeId == null || "".equals(roadmapBigTypeId.toString())) {
                return ResultUtil.error(500, "缺少参数roadmapBigTypeId", "Miss Param roadmapBigTypeId", null);
            }
            if (pcbaCompanyIdsStr == null || "".equals(pcbaCompanyIdsStr)) {
                return ResultUtil.error(500, "缺少参数pcbaCompanyIdsStr", "Miss Param pcbaCompanyIdsStr", null);
            }
            if (year == null || "".equals(year.toString())) {
                return ResultUtil.error(500, "缺少参数year", "Miss Param year", null);
            }
            if (versionY == null || "".equals(versionY.toString())) {
                return ResultUtil.error(500, "缺少参数versionY", "Miss Param versionY", null);
            }
            if (versionN == null || "".equals(versionN)) {
                return ResultUtil.error(500, "缺少参数versionN", "Miss Param versionN", null);
            }
            List<List<RoadmapSmallType>> finalList = new ArrayList<>();
            String[] arr = pcbaCompanyIdsStr.split(",");
            for (String s : arr) {
                PcbaCompany pcbaCompany = pcbaCompanyDao.getPcbaCompanyById(Integer.parseInt(s));
                List<RoadmapSmallType> list = roadmapSmallTypeDao.getAllRoadmapSmallType(null, roadmapBigTypeId);
                for (RoadmapSmallType roadmapSmallType : list) {
                    List<RoadmapParam> allRoadmapParam = roadmapParamDao.getAllRoadmapParam(null, roadmapBigTypeId, roadmapSmallType.getRoadmapSmallTypeId());
                    for (RoadmapParam roadmapParam : allRoadmapParam) {

                        if (roadmapParam.getRoadmapParamPicture() != null && !"".equals(roadmapParam.getRoadmapParamPicture())) {
                            if (env != null && env.contains("pro"))  {
                                roadmapParam.setRoadmapParamPictureBase64(CommonUtils.base64Prefix() + CommonUtils.getImageBase64FromUrl(url + "/" + fileBucket + "/" + roadmapParam.getRoadmapParamPicture()));
                            } else {
                                roadmapParam.setRoadmapParamPictureBase64(CommonUtils.base64Prefix() + CommonUtils.getImageBase64FromUrl(CommonUtils.returnHostPrefixUrl() + roadmapParam.getRoadmapParamPicture()));
                            }
                        }

                        RoadmapCmStatus roadmapCmStatus = roadmapCmStatusDao.getRoadmapCmStatusByCompanyAndVersion(Integer.parseInt(s), versionY, versionN);
                        if (roadmapCmStatus != null) {
                            roadmapParam.setParamsStatus(roadmapCmStatus.getStatus());
                        }

                        roadmapParam.setCompanyName(pcbaCompany.getPcbaCompanyNameCn());
                        // 根据 key 和 工厂id 查询
                        RoadmapCm roadmapCm = roadmapCmDao.getRoadmapCmByCompanyAndKey(Integer.parseInt(s), roadmapParam.getRoadmapParamKey(), versionY, versionN);
                        if (roadmapCm != null) {
                            roadmapParam.setValue1(roadmapCm.getRoadmapCmParamValue1());
                            roadmapParam.setValue2(roadmapCm.getRoadmapCmParamValue2());
                            roadmapParam.setValue3(roadmapCm.getRoadmapCmParamValue3());
                            roadmapParam.setCauses1(roadmapCm.getAbilityMismatchCauses1());
                            roadmapParam.setPlan1(roadmapCm.getAbilityImprovePlan1());
                            roadmapParam.setCauses2(roadmapCm.getAbilityMismatchCauses2());
                            roadmapParam.setPlan2(roadmapCm.getAbilityImprovePlan2());
                            roadmapParam.setCauses3(roadmapCm.getAbilityMismatchCauses3());
                            roadmapParam.setPlan3(roadmapCm.getAbilityImprovePlan3());
                        }

                        ////////////////////为了速度，改为直接从数据库读取、、、、、、、、、、、、、、、、、、
                        // 循环匹配 cm能力
                        // value1 取值 如配置的规则（需要来自规则，不能从数据库读取），否则从数据库读取
                        /*String finalValue = "";
                        if(roadmapParam.getTechDeviceParamKeys() == null || "".equals(roadmapParam.getTechDeviceParamKeys())){
                            if(roadmapCm != null){
                                finalValue = roadmapCm.getRoadmapCmParamValue1();
                            }
                        } else{
                            Result result = coreAlgorithmService.calcRoadmapCmParamValue1(Integer.parseInt(s), roadmapParam.getRoadmapParamKey());
                            if(result.getCode() == 200){
                                finalValue = result.getData().toString();
                            }
                        }
                        roadmapParam.setValue1(finalValue);*/

                        String finalIsMatch1 = "否";
                        String finalIsMatch2 = "否";
                        String finalIsMatch3 = "否";
                        RoadmapH3c roadmapH3c = roadmapH3cDao.getRoadmapH3cByKey(roadmapParam.getRoadmapParamKey(), year, versionY, versionN);
                        if (roadmapH3c != null) {
                            Result result2 = coreAlgorithmService.calcRoadmapCmIsMatchH3c(roadmapParam.getValueAlgorithmType(), roadmapParam.getRuleAlgorithmType(), roadmapH3c.getRoadmapH3cParamValue1(), roadmapCm != null ? roadmapCm.getRoadmapCmParamValue1() : null);
                            if (result2.getCode() == 200) {
                                finalIsMatch1 = result2.getData().toString();
                            }
                            roadmapParam.setIsMatch1(finalIsMatch1);

                            Result result2_ = coreAlgorithmService.calcRoadmapCmIsMatchH3c(roadmapParam.getValueAlgorithmType(), roadmapParam.getRuleAlgorithmType(), roadmapH3c.getRoadmapH3cParamValue2(), roadmapCm != null ? roadmapCm.getRoadmapCmParamValue2() : null);
                            if (result2_.getCode() == 200) {
                                finalIsMatch2 = result2_.getData().toString();
                            }
                            roadmapParam.setIsMatch2(finalIsMatch2);


                            Result result2__ = coreAlgorithmService.calcRoadmapCmIsMatchH3c(roadmapParam.getValueAlgorithmType(), roadmapParam.getRuleAlgorithmType(), roadmapH3c.getRoadmapH3cParamValue3(), roadmapCm != null ? roadmapCm.getRoadmapCmParamValue3() : null);
                            if (result2__.getCode() == 200) {
                                finalIsMatch3 = result2__.getData().toString();
                            }
                            roadmapParam.setIsMatch3(finalIsMatch3);

                            roadmapParam.setValueH3C1(roadmapH3c.getRoadmapH3cParamValue1());
                            roadmapParam.setValueH3C2(roadmapH3c.getRoadmapH3cParamValue2());
                            roadmapParam.setValueH3C3(roadmapH3c.getRoadmapH3cParamValue3());
                        }

                        RoadmapH3c roadmapH3c2 = roadmapH3cDao.getRoadmapH3cByKey(roadmapParam.getRoadmapParamKey(), year + 1, versionY, versionN);
                        if (roadmapH3c2 != null) {
                            roadmapParam.setValueH3C1Add1Year(roadmapH3c2.getRoadmapH3cParamValue1());
                            roadmapParam.setValueH3C2Add1Year(roadmapH3c2.getRoadmapH3cParamValue2());
                            roadmapParam.setValueH3C3Add1Year(roadmapH3c2.getRoadmapH3cParamValue3());
                        }

                        RoadmapH3c roadmapH3c3 = roadmapH3cDao.getRoadmapH3cByKey(roadmapParam.getRoadmapParamKey(), year + 2, versionY, versionN);
                        if (roadmapH3c3 != null) {
                            roadmapParam.setValueH3C1Add2Year(roadmapH3c3.getRoadmapH3cParamValue1());
                            roadmapParam.setValueH3C2Add2Year(roadmapH3c3.getRoadmapH3cParamValue2());
                            roadmapParam.setValueH3C2Add2Year(roadmapH3c3.getRoadmapH3cParamValue3());
                        }

                    }
                    roadmapSmallType.setRoadmapParamList(allRoadmapParam);
                }
                finalList.add(list);
            }
            return ResultUtil.success(finalList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
